<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 企业管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="danger"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                    :disabled="isDisabled"
                >批量删除</el-button>
                <el-button
                     type="success"
                     icon="el-icon-plus"
                     class="handle-del mr10"
                     @click="handleAdd"            
                >添加</el-button>
                <!-- <el-select v-model="query.address" placeholder="地址" class="handle-select mr10">
                    <el-option key="1" label="广东省" value="广东省"></el-option>
                    <el-option key="2" label="湖南省" value="湖南省"></el-option>
                </el-select> -->
                <el-input v-model="query.enterName" placeholder="请输入企业名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                v-loading="loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0,0,0,0.8)"
                :data="enterList"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <!-- <el-table-column prop="enterId" label="企业ID" width="55" align="center"></el-table-column> -->
                <el-table-column prop="enterName" label="企业名称" align="center"></el-table-column>
                <el-table-column prop="address" label="地址" align="center"></el-table-column>
                <el-table-column prop="contact" label="联系人" align="center"></el-table-column>
                <el-table-column prop="registerTime" label="注册时间" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!--  添加编辑弹出框  -->
        <el-dialog :title="title" :visible.sync="visible" width="30%">
           <el-form :model="form" :rules="rules" ref="form" label-width="70px">
                <el-form-item label="名称" prop="enterName">
                    <el-input v-model="form.enterName"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
                <el-form-item label="联系人" prop="contact">
                    <el-input v-model="form.contact"></el-input>
                </el-form-item>
                <el-form-item label="简介" prop="introduce">
                    <el-input  type="textarea" v-model="form.introduce"></el-input>
                </el-form-item>
           </el-form>

           <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </span>
        </el-dialog>

        
        
    </div>
</template>

<script>
import {getList,delEnter,getEnter,updateEnter,addEnter,delAllEnter} from "@/api/basic/enter"


export default {
    name: "Enter",
    data () {
        return {
            query: {
                enterName: '',
                pageIndex: 1,
                pageSize: 3
            },
            enterList: [],
            multipleSelection: [],
            visible: false,
            title: '',
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            loading: false,
            rules: {
                enterName: [
                    {required: true, message: '请填写企业名称',trigger: 'blur'}
                ],
                address: [
                    {required: true, message: '请填写地址',trigger: 'blur'}
                ],
                contact: [
                    {required: true, message: '请填写联系人',trigger: 'blur'}
                ]
            },
            isDisabled: true
        }
    },
    created () {
        // 获取后台的数据
        this.getData();
        
    },
    watch: {
        // 监听最后一页删除最后一条数据时，当前页不变情况
        pageTotal(){
            if(this.pageTotal==(this.query.pageIndex-1)*this.query.pageSize && this.pageTotal!=0){
                this.query.pageIndex-=1;
                this.getData();
            }
        }
    },
    methods: {
        // 获取数据
        getData(){
            this.loading = true;
            getList(this.query).then(res => {
                this.enterList = res.data.records;
                this.pageTotal = res.data.total;
                this.loading = false;
            })
        },
        // 触发搜索按钮
        handleSearch(){
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除按钮
        handleDelete(index,row){
            this.$confirm('确定要删除吗?','提示',{
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: 'warning'
            }).then(()=>{
                // return del(row.enterId);
                delEnter(row.enterId).then(res=>{
                    this.$message.success('删除成功')
                    this.getData();
                })
                
            }).catch(()=>{})
        },
        // 多选操作
        handleSelectionChange(val){
            this.multipleSelection = val;
            if(this.multipleSelection.length === 0){
                this.isDisabled = true;
            }else{
                this.isDisabled = false;
            }
        },
        // 批量删除
        delAllSelection(){
            this.$confirm('确定要删除吗?','提示',{
                type: 'warning'
            }).then(()=>{
                const length = this.multipleSelection.length;
                let enterIds = '';
                for (let i = 0; i < length-1; i++) {
                    enterIds += this.multipleSelection[i].enterId + ',';
                }
                
                enterIds += this.multipleSelection[length-1].enterId;
                delAllEnter(enterIds).then(res=>{
                    this.$message.success('删除成功');
                    this.multipleSelection = [];
                    this.getData();
                })
            }).catch(()=>{
                
            })
        },
        // 添加按钮
        handleAdd(){
            this.reset();
            this.title = "添加企业";
        },
        // 编辑按钮
        handleEdit(index,row){
            this.reset();
            getEnter(row.enterId).then(res => {
                this.form = res.data;
                this.title = "编辑企业";
            })
        },
        // 提交按钮
        submitForm(){
            this.$refs["form"].validate(valid => {
                if(valid){
                    if(this.form.enterId != undefined){
                        // 更新企业
                        updateEnter(this.form).then(res=>{
                            this.$message.success(res.message);
                            this.visible = false;
                            this.getData();
                        })
                    }else{
                        // 添加企业
                        addEnter(this.form).then(res=>{
                            this.$message.success(res.message);
                            this.visible = false;
                            this.getData();
                        })
                    }
                }
            })
            
        },
        // 分页导航
        handlePageChange(val){
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        cancel(){
            this.reset();
            this.visible = false;
        },
        // 表单重置
        reset(){
            this.visible = true;
            this.form = {
                enterName: undefined,
                address: undefined,
                contact: undefined
            }
             this.$nextTick(()=>{
                    this.$refs.form.resetFields();
            })     
        }
    }

}
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>